let number = function (val) {
  let rules = (rule, value, callback) => {
    if (value != "" && value != null) {
      if (/^\d+$/.test(value) == false) {
        callback(new Error("必须为数字值"));
      } else {
        callback();
      }
    } else {
      if (val) {
        callback(new Error(val + "不能为空"));
      } else {
        callback();
      }
    }
  }
  return rules
}

let required = function (val) {
  let rules = (rule, value, callback) => {
    if (value != "" && value != null) {
      callback();
    } else {
      callback(new Error(val + "不能为空"));
    }
  }
  return rules
}


let email = function (val) {
  let rules = (rule, value, callback) => {
    if (value != "" && value != null) {
      if (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value) == false) {
        callback(new Error("邮箱格式不合法"));
      } else {
        callback();
      }
    } else {
      if (val) {
        callback(new Error(val + "不能为空"));
      } else {
        callback();
      }
    }
  }
  return rules
}

let phone = function (val) {
  let rules = (rule, value, callback) => {
    if (value != "" && value != null) {
      if (/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/.test(value) == false) {
        callback(new Error("手机号格式不合法"));
      } else {
        callback();
      }
    } else {
      if (val) {
        callback(new Error(val + "不能为空"));
      } else {
        callback();
      }
    }
  };
  return rules
}

let stringLength = function (val) {
  let rules = (rule, value, callback) => {
    if (value != '' && value != null) {
      if (value.length > val[1] || value.length < val[0]) {
        callback(new Error("字数请在" + val[0] + '和' + val[1] + '位之间'));
      } else {
        callback();
      }
    } else {
      if (val.length > 2) {
        callback(new Error(val[2] + "不能为空"));
      } else {
        callback();
      }
    }

  };
  return rules
}


let numberMin_Max = function (val) {
  let rules = (rule, value, callback) => {
    if (value != '' && value != null) {
      if (/^\d+$/.test(value) == false) {
        callback(new Error("必须为数字值"));
      } else {
        if (value > val[1] || value < val[0]) {
          callback(new Error("数值范围请在" + val[0] + '和' + val[1] + '之间'));
        } else {
          callback();
        }
      }
    } else {
      if (val.length > 2) {
        callback(new Error(val[2] + "不能为空"));
      } else {
        callback();
      }
    }

  };
  return rules
}


export default {
  number,
  email,
  phone,
  stringLength,
  numberMin_Max,
  required
}
